(function(angular) {
	'use strict';
	angular.module('fs.waf').factory('deliMessageService', deliMessageService);
	deliMessageService.$inject = [ '$q', 'loggerFactory', 'commonService', 'ajaxServiceFactory', 'modalService', 'gridService', 'arithService', 'formatService', 'storageService', '$filter'];
	function deliMessageService($q, loggerFactory, commonService, ajaxServiceFactory, modalService, gridService, arithService, formatService, storageService, $filter) {
		var log = loggerFactory.getLogger("deliMessageService"), ajaxService = ajaxServiceFactory.buildAjaxService({
			getDeliMessageOptions : {
				url : 'do/cda/dataList/deliMessageOptions',
				method : 'post'
			},
			getDeliMessageListSearchOptions : {
				url : 'do/cda/dataList/deliMessageListSearchOptions',
				method : 'post'
			},
			getDeliMessageTreeData : {
				url : 'do/cda/deliMessage/obtainTreeData',
				method : 'post'
			}
		});
		
		function initGeneral($scope, ctrl, editProcessFlag) {
			var defer = $q.defer();
			ajaxService.getDeliMessageOptions().then(function(response) {
				ctrl.yesNoOptions = response.data.yesNoOptions;
				ctrl.localeCodeOptions = response.data.localeCodeOptions;
				ctrl.msgcatCodeOptions = response.data.msgcatCodeOptions;
				ctrl.deliveryTypeOptions = response.data.deliveryTypeOptions;
				
				defer.resolve();
			}, angular.noop);
			// for attachment
			ctrl.getAttachmentList = function() {
				if (ctrl.deliMessage) {
					var params = {
						'srcTxnType' : ctrl.deliMessage.currentDataType,
						'srcTxnRef' : $filter('encode')(ctrl.deliMessage.deliMessageId),
						'entity' :  ctrl.deliMessage,
						'isViewPage': !editProcessFlag,
						'storeTempTableFlag' : true,
						'relatedDocIdsProp': 'tempDocIds'//the default relatedDocIdsProp is tempDocIds, it is the property in the voCompany. 
					};
					commonService.getAttachmentList(params);
				}
			};
		
			return defer.promise;
		}
		
		function initListSearchOptions($scope, ctrl) {
			var defer = $q.defer();
			ajaxService.getDeliMessageListSearchOptions().then(function(response) {
				ctrl.yesNoOptions = response.data.yesNoOptions;
				ctrl.deliveryTypeOptions = response.data.deliveryTypeOptions;
				ctrl.serviceCodeOptions = response.data.serviceCodeOptions;
				ctrl.deliveryFolderOptions = response.data.deliveryFolderOptions;
				ctrl.deliveryFolderChanged = function(value) {
					if (ctrl.searchCriteria.deliveryFolder == 'S') {
//						var siteCode = storageService.getUserInfo().siteCode ;
						var workingCompanyCode = storageService.getUserInfo().workingCompanyCode ;
						ctrl.searchCriteria.fromCompany = workingCompanyCode;
						ctrl.searchCriteria.toCompany = '';
					} else if (ctrl.searchCriteria.deliveryFolder == 'R') {
						var workingCompanyCode = storageService.getUserInfo().workingCompanyCode ;
						ctrl.searchCriteria.toCompany = workingCompanyCode;
						ctrl.searchCriteria.fromCompany = '';
					}
				};
				
				defer.resolve();
			}, angular.noop);
		
			return defer.promise;
		}
		
		return {
			initGeneral : initGeneral,
			initListSearchOptions : initListSearchOptions,
		};
	}
})(angular);